class Image < ActiveRecord::Base
  require 'RMagick'
  
  MIME_TYPES = {
    'image/jpeg' => 'jpg',
    'image/png' => 'png',
    'image/gif' => 'gif',
  }
  
  attr_accessor :r_image, :width, :height
  
  def initialize(image_path)
    self.load(image_path)
  end
  
  def load(image_path)
    #read always reads an array. So only use the first item
    @r_image = Magick::Image::read(image_path).first
  end
  
  def extension
    MIME_TYPES[@r_image.mime_type] || (raise Exception, 'This is not an image!')
  end
  
  def aspectratio
    (self.width.to_f / self.height.to_f).to_f
  end
  
  def width
    @r_image.columns
  end
  
  def height
    @r_image.rows
  end
  
  def method_missing(methId, *args)
    @r_image.send(methId, *args)
  end
  
  def cdate
    File.ctime @r_image.filename  
  end
  
end